home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / crc113.zip / CALC.C next >
Text File  |  1990-01-02  |  1KB  |  57 lines

  1. #include "calc.h"
  2.  
  3. #define TABLE_SIZE  256
  4.  
  5. static unsigned int crc16tab[TABLE_SIZE];
  6. static unsigned long crc32tab[TABLE_SIZE];
  7.  
  8. void make_table(void)
  9. {
  10.   int i, inx;
  11.   int carry16, carry32;
  12.   unsigned int entry16;
  13.   unsigned long entry32;
  14.  
  15.   for (inx = 0; inx < TABLE_SIZE; ++inx) {
  16.     entry16 = inx;
  17.     entry32 = inx;
  18.  
  19.     for (i = 0; i < 8; ++i) {
  20.       carry16 = entry16 & 1;
  21.       entry16 >>= 1;
  22.       if (carry16)
  23.         entry16 ^= 0xa001;
  24.  
  25.       carry32 = entry32 & 1;
  26.       entry32 >>= 1;
  27.       if (carry32)
  28.         entry32 ^= 0xedb88320;
  29.     }
  30.  
  31.     crc16tab[inx] = entry16;
  32.     crc32tab[inx] = entry32;
  33.   }
  34. }
  35.  
  36. void update_crc(char *buf,
  37.                 int count,
  38.                 unsigned int *crc16,
  39.                 unsigned long *crc32)
  40. {
  41.   int i;
  42.   unsigned char inx16, inx32;
  43.  
  44.   if (count == 0)
  45.     return;
  46.  
  47.   for (i = 0; i < count; ++i) {
  48.     inx16 = buf[i] ^ *crc16;
  49.     *crc16 >>= 8;
  50.     *crc16 ^= crc16tab[inx16];
  51.  
  52.     inx32 = buf[i] ^ *crc32;
  53.     *crc32 >>= 8;
  54.     *crc32 ^= crc32tab[inx32];
  55.   }
  56. }
  57.